package com.cskaoyan.service;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.util.LocalStorage;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Ramsey
 * @date 2023年05月29日 9:04
 */
public class AdminStorageServiceimpl implements AdminStorageService {
    private LocalStorage storage = new LocalStorage();

    @Override
    public MarketStorage store(InputStream inputStream, long size, String contentType, String fileName) {
        String key = generateKey(fileName);
        storage.store(inputStream, size, contentType, key);
        String url = generateUrl(key);
        MarketStorage storageInfo = new MarketStorage();
        storageInfo.setDeleted(false);
        storageInfo.setName(fileName);
        storageInfo.setSize((int) size);
        storageInfo.setType(contentType);
        storageInfo.setKey(key);
        storageInfo.setUrl(url);
        this.add(storageInfo);

        return storageInfo;
    }

    @Override
    public int add(MarketStorage storageInfo) {
        Date now = new Date();
        storageInfo.setAddTime(now);
        storageInfo.setUpdateTime(now);
        // MarketStorageMapper storageMapper = TransactionUtil.getMapper(MarketStorageMapper.class);2023年6月2日14:55:41更改
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper storageMapper = sqlSession.getMapper(MarketStorageMapper.class);
        int insert = storageMapper.insert(storageInfo);
        sqlSession.commit();
        return insert;
    }

    private String generateUrl(String key) {
        // 这个前缀也可以写在配置文件中
        return "http://localhost:8083/wx/storage/fetch/" + key;
    }

    private String generateKey(String originalFilename) {
        int index = originalFilename.lastIndexOf('.');
        String suffix = originalFilename.substring(index);
        String key = UUID.randomUUID().toString().replaceAll("-", "") + suffix;

        return key;
    }


    //下面是新增的2023年6月1日21:52:58

    @Override
    public List<MarketStorage> list(String key, Integer page, Integer limit, String sort, String order, String name) {
        TransactionUtil.startTransaction();
        MarketStorageMapper storageMapper = TransactionUtil.getMapper(MarketStorageMapper.class);
        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();

        //新增根据name查询()模糊查询
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike(name);
        }

        if (!StringUtil.isEmpty(key)) {
            criteria.andKeyEqualTo(key);
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return storageMapper.selectByExample(example);
    }

    @Override
    public BaseRespVo update(MarketStorage storage) {
        TransactionUtil.startTransaction();
        MarketStorageMapper storageMapper = TransactionUtil.getMapper(MarketStorageMapper.class);

        BaseRespVo isError = validate(storage);
        if (isError != null) {
            return isError;
        }
        Date now = new Date();
        storage.setAddTime(now);
        storage.setUpdateTime(now);
        int update = storageMapper.updateByPrimaryKeySelective(storage);
        if (update == 0) {
            return BaseRespVo.fail(852, "更新数据失败");
        }
        TransactionUtil.commit();

        return BaseRespVo.ok();
    }

    private BaseRespVo validate(MarketStorage storage) {
        /*
        key&name&type&size is not null
         */
        String key = storage.getKey();
        if (StringUtil.isEmpty(key)) {
            return BaseRespVo.badArgument();
        }
        String name = storage.getName();
        if (StringUtil.isEmpty(name)) {
            return BaseRespVo.badArgument();
        }
        String type = storage.getType();
        if (StringUtil.isEmpty(type)) {
            return BaseRespVo.badArgument();
        }
        Integer size = storage.getSize();
        if (size == null || size < 0) {
            return BaseRespVo.badArgument();
        }
        return null;
    }


    @Override
    public BaseRespVo delete(MarketStorage storage) {
        TransactionUtil.startTransaction();
        MarketStorageMapper storageMapper = TransactionUtil.getMapper(MarketStorageMapper.class);
        BaseRespVo isError = validate(storage);
        if (isError != null) {
            return isError;
        }

        int delete = storageMapper.deleteByPrimaryKey(storage.getId());
        if (delete == 0) {
            return BaseRespVo.fail(963, "删除失败");
        }
        TransactionUtil.commit();
        return BaseRespVo.ok();
    }
}
